#!/bin/bash

# Copyright 2019 The Vitess Authors.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script is a sample hook to test the backup transform.
# Upon write, it adds a header line with the single word 'header'.
# Upon read, it removes the header, and checks it's 'header'.
# In any case, it then copies the data, by execing 'cat'.
# Any error is displayed to stderr, and triggers an 'exit 1'.

while [[ $# -gt 1 ]]; do
  key="$1"

  case $key in
    -operation)
      OPERATION="$2"
      shift # past argument
      ;;
    *)
      echo "unknown command line parameter:" $key 1>&2
      exit 1
      ;;
  esac
  shift # past argument or value
done

if [ "$OPERATION" == "write" ]; then
  echo "header"
  echo "normal write" 1>&2
elif [ "$OPERATION" == "read" ]; then
  read header
  if [ "$header" != "header" ]; then
    echo "invalid header:" $header 1>&2
    exit 1
  fi
  echo "normal read" 1>&2
else
  echo "invalid operation:" $OPERATION 1>&2
  exit 1
fi
  
exec cat
